/**
 * 
 */
package umacis.data.elements;

import java.util.Enumeration;
import java.util.Vector;



import com.sun.lwuit.Command;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.SelectionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.list.DefaultListCellRenderer;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.util.Log;

/**
 * @author Administrator
 *
 */
public class Menu extends Dialog implements ActionListener, SelectionListener {
	//private Class ownerClass;
	public String location;
	public String name = ""; 
	public Vector menuActions;
	
	private String nameSelect = "Select", nameCancel = "Cancel";
	private DefaultListModel menuDefaultListModel = null;
	private List menuList;
	private boolean isChanged = true;
	private boolean isLinked;
	//private MenuListener listener;
	
	//public Actions liveActions;
	public Menu() {
		
	}

	public boolean isDialog() {
		return true;
	}
	
//	public Menu( Class ownerClass, String menuName) {
//
//		this.name = menuName;
//		//this.ownerClass = ownerClass;
//		actions = new Vector();
//	}
	
	/**
	 * ownerClass still to be set
	 * @param menuName
	 */
	public Menu( String menuName) {

		this.name = menuName;
		menuActions = new Vector();
	}
	
//	public void addAction(Handle actionHandle) { 
//		actions.addElement(new Action(null, null, actionHandle));
//	}
	
//	public void add(String actionName) { 
//		menuActions.addElement(new Action(name, location, new Handle(actionName)));
//		isChanged = true;
//	}
	
	public void add(Menu menu ) { 
		menuActions.addElement(new Action(location, menu));
		isChanged = true;
	}


	public void add(Class screenToShowClass) {
		menuActions.addElement(new Action(screenToShowClass.getName(), (String)null));
		isChanged = true;
	}
	
//	public void add(Handle handle) {
//		menuActions.addElement(new Action("", location, handle));
//		isChanged = true;
//	}
	
	private void makeMenu() {
		setLayout(new BorderLayout());
		//setTransitionInAnimator(transitionIn);
        //setTransitionOutAnimator(transitionOut);
		//setDialogStyle(menuStyle);
      
        setScrollable(false);
        
		menuDefaultListModel = new DefaultListModel();
		
		menuList = new List(menuDefaultListModel);
		
		menuList.addSelectionListener(this);
		menuList.addActionListener(this); 
		this.addKeyListener('9', this);
		menuList.setListCellRenderer(new DefaultListCellRenderer(false));
//		menuList.setFixedSelection(List.FIXED_NONE);
//		menuList.setSmoothScrolling(true);
		
		getContentPane().getStyle().setMargin(10, 10, 10, 10);
        addComponent(BorderLayout.CENTER, menuList);
		//addComponent(menuList);
		setTitle(name);
		addCommand(new Command(nameSelect));
		addCommand(new Command(nameCancel));
		//menuDefaultListModel.removeAll();
		
	}

	public void removeAllItems() {
		menuActions.removeAllElements();
		isChanged = true;
		isLinked = false;
	}
		
	public void showMenu() {
		if (menuDefaultListModel==null) makeMenu();
		if (isChanged) populateWithItems();
		Log.p("Menu.show: showing menu "+name, Log.INFO);
//		int marginLeft = (int) (Form.this.getWidth() * 0.25f);
////        int marginRight = 0;
////        if (isReverseSoftButtons()) {
////            marginRight = marginLeft;
////            marginLeft = 0;
////        }
//        int height = Form.this.getHeight() / 2;
        //return this.showshow(height, 0, marginLeft, marginRight, true);
		Command cmd = super.showDialog(); //show(BorderLayout.CENTER, true);
		Log.p(cmd.toString(), Log.DEBUG);
		//Command command = evt.getCommand();
//		String commandName = nameSelect;
//		if (command!=null) {
		String commandName = cmd.getCommandName();
		Log.p("Menu.actionPerformed: " +commandName+" " + menuDefaultListModel.getSelectedIndex(), Log.INFO);
////		}
//		else { Umacis.log("Menu: Selindex = " + menuDefaultListModel.getSelectedIndex());
//		dispose();
		
		//Umacis.getInstance().showCurrentScreen();     //XXXget rid of menu and redraw..
        //dispose(); not after showCurrentScreen!!!
        if (commandName.equals(nameSelect)) {
        	int selIndex = menuDefaultListModel.getSelectedIndex();
        	Action menuAction = ((Action)menuActions.elementAt(selIndex));
        	menuAction.menuIndex = selIndex;
        	Umacis.getInstance().executeMenuAction(menuAction);
        	//listener.menuItemSelected(name, menuAction, selIndex);
        	
        }
		
	}
	
	private void populateWithItems() {
		menuDefaultListModel.removeAll();
		Enumeration e = menuActions.elements();
		while (e.hasMoreElements()) {
			menuDefaultListModel.addItem(e.nextElement());
		}
		isChanged  = false;
		isLinked = false;
		
	}

	
	public String toString() {
		return name;
	}

//	public void clearHandles() {
//		Enumeration e = actions.elements();
//		while (e.hasMoreElements()) ((Action)e.nextElement()).setHandle(null);
//	}

	public void link(Action[] actions) {
		if (!isLinked) { 
			Log.p("Menu.link: linking actions to live actions in " + name, Log.INFO);
			Enumeration e = menuActions.elements();
			
			while (e.hasMoreElements()) {
				Action actionToFind = (Action)e.nextElement();
				if (actionToFind.type == Action.HANDLE)  actionToFind.index = getIndex(actions, actionToFind);
			}
			Log.p("Menu.link: finished linking actions to live actions in " + name, Log.INFO);
			isLinked = true;
		}
		else {
			Log.p("Menu.link: this menu is already linked: " + name, Log.INFO);
		}
	}

	private int getIndex(Action[] actions, Action action) {
	int length = actions.length;
	for (int i=0; i<length; i++) {
		if (actions[i].equals(action)) return i;
	}
	return -1;
}

	public Action elementAt(int index) {
		return (Action) menuActions.elementAt(index);
	}

	
	public void keyReleased( int keyCode) {
		//listener.menuKeyReleased(name, keyCode);
		super.keyReleased(keyCode);
	}

	public void selectionChanged(int oldSelected, int newSelected) {
		Log.p("from selectionChanged:" + String.valueOf(oldSelected) + " " + String.valueOf(newSelected), Log.INFO);
	}

	//menu event
	public void actionPerformed(ActionEvent evt) {
		Log.p(evt.toString(), Log.INFO);
		return; //XXX implement responding to select key as a SELECT event and also to the keys perhaps....
//			Command command = evt.getCommand();
//			String commandName = nameSelect;
//			if (command!=null) {
//			commandName = command.getCommandName();
//			Umacis.log("Menu.actionPerformed: commandName+"+ menuDefaultListModel.getSelectedIndex());
//			}
//			else { Umacis.log("Menu: Selindex = " + menuDefaultListModel.getSelectedIndex());
//			dispose();
//			
//			Umacis.getInstance().showCurrentScreen();     //XXXget rid of menu and redraw..
//	        //dispose(); not after showCurrentScreen!!!
//	        if (commandName.equals(nameSelect)) {
//	        	int selIndex = menuDefaultListModel.getSelectedIndex();
//	        	Action menuAction = ((Action)menuActions.elementAt(selIndex));
//	        	menuAction.menuIndex = selIndex;
//	        	Umacis.getInstance().executeMenuAction(menuAction);
//	        	//listener.menuItemSelected(name, menuAction, selIndex);
//	        	
//	        }
//	        		
//			}  
	}
	

//	public void addMenuListener(MenuListener listener) {
//		this.listener = listener;
//		
//	}
	

		
		
}
		
